package demoxsgl_300.com.jj0588;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.siberiadante.customdialoglib.EditDialog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import demoxsgl_300.com.jj0588.adapter.Event_Adapter;
import demoxsgl_300.com.jj0588.bean.MyEvent;
import demoxsgl_300.com.jj0588.utils.FileUtils;
import demoxsgl_300.com.jj0588.utils.SpfUtils;
import me.drakeet.materialdialog.MaterialDialog;
import okhttp3.Call;


public class MUluActivity extends AppCompatActivity {
    Event_Adapter work_adapter;
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.lv_list)
    ListView lvList;
    @BindView(R.id.button3)
    Button button3;
    @BindView(R.id.button)
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mulu);
        EventBus.getDefault().register(this);
        ButterKnife.bind(this);
        work_adapter = new Event_Adapter();
        lvList.setAdapter(work_adapter);
        shuaxin();
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                Toast.makeText(MUluActivity.this, "cao", Toast.LENGTH_SHORT).show();
                String item = (String) work_adapter.getItem(i);
                String url = path + "/" + item;
                Log.e("888", url);
                cc2(url);

            }
        });
        lvList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                String item = (String) work_adapter.getItem(i);
                String url = path + "/" + item;
                shangcuuan(url, item);
                return true;
            }
        });
    }

//    @OnClick(R.id.button3)
//    public void onViewClicked() {
//        cc();
//
//    }

    @Override

    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 处理数据分发
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(MyEvent event) {

        if (!TextUtils.isEmpty(event.getSc())) {
            if (event.getSc().equals("finish")) {
//                String mac = event.getMAC();
//                textView.setText(mac);
//                String weather = event.getWeather();
//
//                String area = event.getAREA();
//                textView2.setText(area+weather);
//                String time = event.getTime();
//                textView3.setText(time);
                shuaxin();

            }
        }
    }

    Vector<String> strings;
    String path;

    public void shuaxin() {
        work_adapter.clear();
        path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).toString();
        strings = FileUtils.GetVideoFileName(path);
        work_adapter.addItem(strings);
    }

    MaterialDialog alert;

    public void cc() {
        final ArrayAdapter<String> arrayAdapter
                = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1);

        arrayAdapter.add("手动模式");
        arrayAdapter.add("定时模式");
        arrayAdapter.add("自动模式");

        ListView listView = new ListView(this);
        listView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        float scale = getResources().getDisplayMetrics().density;
        int dpAsPixels = (int) (8 * scale + 0.5f);
        listView.setPadding(0, dpAsPixels, 0, dpAsPixels);
        listView.setDividerHeight(0);
        listView.setAdapter(arrayAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i) {
                    case 0:
                        startActivity(new Intent(MUluActivity.this, Record_SD_Activity.class));
                        alert.dismiss();
                        break;
                    case 1:
                        showEditDialog();
                        alert.dismiss();
                        break;
                    case 2:
                        startActivity(new Intent(MUluActivity.this, Record_ZD_Activity.class));
                        alert.dismiss();
                        break;

                    default:
                        break;
                }

            }
        });

        alert = new MaterialDialog(this).setTitle(
                "请选择录制模式").setContentView(listView);

        alert.setPositiveButton("OK", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alert.dismiss();
            }
        });

        alert.show();
    }

    public void cc2(final String url) {
        final ArrayAdapter<String> arrayAdapter
                = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1);

        arrayAdapter.add("全屏");
        arrayAdapter.add("16：9");


        ListView listView = new ListView(this);
        listView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        float scale = getResources().getDisplayMetrics().density;
        int dpAsPixels = (int) (8 * scale + 0.5f);
        listView.setPadding(0, dpAsPixels, 0, dpAsPixels);
        listView.setDividerHeight(0);
        listView.setAdapter(arrayAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i) {
                    case 0:
                        Intent intent1 = new Intent(MUluActivity.this, HPlayerActivity.class);
                        intent1.putExtra("url", url);
//
                        intent1.putExtra("type", "1");
                        startActivity(intent1);
                        alert.dismiss();
                        break;
                    case 1:
                        Intent intent = new Intent(MUluActivity.this, HPlayerActivity.class);
                        intent.putExtra("url", url);
                        intent.putExtra("type", "2");
//
                        startActivity(intent);
                        alert.dismiss();
                        break;


                    default:
                        break;
                }

            }
        });

        alert = new MaterialDialog(this).setTitle(
                "请选择缩放类别").setContentView(listView);

        alert.setPositiveButton("OK", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alert.dismiss();
            }
        });

        alert.show();
    }

    /**
     * EditDialog
     */
    private void showEditDialog() {
        final EditDialog dialog = new EditDialog(MUluActivity.this).builder();
        dialog.setTitle("请输入秒數");
        dialog.setCancelable(false);
        dialog.setNegativeButton("取消", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
        dialog.setPositiveButton("确定", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String msg = dialog.getMsg();
                Intent intent = new Intent(MUluActivity.this, Record_DS_Activity.class);
                intent.putExtra("time", msg);
                startActivity(intent);
//                Toast.makeText(getActivity(), "输入内容为：" + msg, Toast.LENGTH_LONG).show();


                dialog.dismiss();

            }
        });
        dialog.show();
    }


    /**
     * 上传文件
     *
     * @param
     */
    public void uploadFile(String files, String item) {

        File file = new File(files);
        if (!file.exists()) {
            Toast.makeText(MUluActivity.this, "文件不存在，请修改文件路径", Toast.LENGTH_SHORT).show();
            return;
        }
        Map<String, String> params = new HashMap<>();

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        Date now = new Date();
        String parse = sdf.format(now);

        String ip = (String) SpfUtils.get(MUluActivity.this, "ip", "192.168.43.1");
        String url = "http://" + ip + ":8080/UpData/Fileupdat";
        Log.e("888", url);

        OkHttpUtils.post()//
                .addFile("mFile", item, file)//
                .url(url)//
                .params(params)//

                .build()//
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("8881", "xx" + e);
                        Toast.makeText(MUluActivity.this, "上传失败" + e.toString(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("8882", "xx" + response);
                        Toast.makeText(MUluActivity.this, "上传成功!", Toast.LENGTH_SHORT).show();
                    }
                });
    }

    MaterialDialog mMaterialDialog;

    public void shangcuuan(final String s, final String item) {
        mMaterialDialog = new MaterialDialog(this)
                .setTitle("请确认是否上传此文件")

                .setPositiveButton("OK", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mMaterialDialog.dismiss();
                        uploadFile(s, item);
                    }
                })
                .setNegativeButton("CANCEL", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mMaterialDialog.dismiss();

                    }
                });

        mMaterialDialog.show();
    }

    @OnClick({R.id.button3, R.id.button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.button3:
                cc();

                break;
            case R.id.button:
                startActivity(new Intent(this, AddKCXX3_Activity.class));

                break;
        }
    }
}

